package org.jeecg.modules.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.flow.entity.ActHiTaskInstance;
import org.jeecg.modules.flow.mapper.ActHiTaskInstanceMapper;
import org.jeecg.modules.flow.query.HistoryTaskQuery;
import org.jeecg.modules.flow.service.IActHiTaskInstanceService;
import org.jeecg.modules.flow.vo.HistoryTaskInstanceVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static org.jeecg.modules.flow.common.util.FlowUtils.beginOfDay;
import static org.jeecg.modules.flow.common.util.FlowUtils.endOfDay;

/**
 * @Description: act_hi_taskinst
 * @Author: jeecg-boot
 * @Date: 2021-12-30
 * @Version: V1.0
 */
@Service
public class ActHiTaskInstanceServiceImpl extends ServiceImpl<ActHiTaskInstanceMapper, ActHiTaskInstance> implements IActHiTaskInstanceService {

    @Resource
    private ActHiTaskInstanceMapper actHiTaskInstanceMapper;

    @Override
    public Page<HistoryTaskInstanceVo> queryHistoricTaskInstances(HistoryTaskQuery query, Integer pageNo, Integer pageSize) {
        QueryWrapper<ActHiTaskInstance> queryWrapper = new QueryWrapper<>();
        Date startTimeBegin = beginOfDay(query.getStartTimeBegin());
        Date startTimeEnd = endOfDay(query.getStartTimeEnd());
        queryWrapper.eq(StringUtils.isNotBlank(query.getTaskId()), "RES.ID_", query.getTaskId())
                .eq(StringUtils.isNotBlank(query.getProcessInstanceId()), "RES.PROC_INST_ID_", query.getProcessInstanceId())
                .eq(StringUtils.isNotBlank(query.getAssignee()), "RES.ASSIGNEE_", query.getAssignee())
                .eq(StringUtils.isNotBlank(query.getBusinessKey()), "HPI.BUSINESS_KEY_", query.getBusinessKey())
                .gt(startTimeBegin != null, "RES.START_TIME_", startTimeBegin)
                .lt(startTimeEnd != null, "RES.START_TIME_", startTimeEnd)
                .orderByAsc("RES.START_TIME_");
        return actHiTaskInstanceMapper.queryHistoricTaskInstances(queryWrapper, new Page<>(pageNo, pageSize));
    }


}
